﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;
namespace Human
{
	public class frmQuaTrinhCongTac : System.Windows.Forms.Form
	{
        AccessData mDB = new AccessData();
        Language lan = new Language();
        string m_user = "",tungay="",denngay="",s_bophan="",user_dn="";
		private DataSet ds =new DataSet();
        DataSet ds_hoten = new DataSet();
        ExportToExcel exp=new ExportToExcel();
		//private Print rpt=new Print();
		private bool bClear =true;
		private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txt_hoten;
		private System.Windows.Forms.Button butThoat;
		private System.Windows.Forms.Button butIn;
		private System.Windows.Forms.Button butXem;
        private Button btnexel;
        private TextBox txt_manv;
        private Label label3;
        private haison haison1;
        private LibList.List lst_manv;
        
		private System.ComponentModel.Container components = null;

		public frmQuaTrinhCongTac(string s_makp,string s_userdn)
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
            s_bophan = s_makp;
            user_dn = s_userdn;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmQuaTrinhCongTac));
            this.label1 = new System.Windows.Forms.Label();
            this.txt_hoten = new System.Windows.Forms.TextBox();
            this.butThoat = new System.Windows.Forms.Button();
            this.butIn = new System.Windows.Forms.Button();
            this.butXem = new System.Windows.Forms.Button();
            this.btnexel = new System.Windows.Forms.Button();
            this.txt_manv = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.lst_manv = new LibList.List();
            this.haison1 = new Human.haison();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(102, 89);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(45, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Họ tên :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txt_hoten
            // 
            this.txt_hoten.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txt_hoten.Location = new System.Drawing.Point(149, 84);
            this.txt_hoten.MaxLength = 50;
            this.txt_hoten.Name = "txt_hoten";
            this.txt_hoten.Size = new System.Drawing.Size(200, 22);
            this.txt_hoten.TabIndex = 2;
            this.txt_hoten.TextChanged += new System.EventHandler(this.txt_hoten_TextChanged);
            this.txt_hoten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_hoten_KeyDown);
            // 
            // butThoat
            // 
            this.butThoat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butThoat.Image = ((System.Drawing.Image)(resources.GetObject("butThoat.Image")));
            this.butThoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThoat.Location = new System.Drawing.Point(267, 127);
            this.butThoat.Name = "butThoat";
            this.butThoat.Size = new System.Drawing.Size(60, 28);
            this.butThoat.TabIndex = 10;
            this.butThoat.Text = "Thoát";
            this.butThoat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butThoat.Click += new System.EventHandler(this.butThoat_Click);
            // 
            // butIn
            // 
            this.butIn.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butIn.Image = ((System.Drawing.Image)(resources.GetObject("butIn.Image")));
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(135, 127);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(60, 28);
            this.butIn.TabIndex = 9;
            this.butIn.Text = "In";
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // butXem
            // 
            this.butXem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butXem.Image = ((System.Drawing.Image)(resources.GetObject("butXem.Image")));
            this.butXem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butXem.Location = new System.Drawing.Point(69, 127);
            this.butXem.Name = "butXem";
            this.butXem.Size = new System.Drawing.Size(60, 28);
            this.butXem.TabIndex = 8;
            this.butXem.Text = "  Xem";
            this.butXem.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butXem.Click += new System.EventHandler(this.butXem_Click);
            // 
            // btnexel
            // 
            this.btnexel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnexel.Image = ((System.Drawing.Image)(resources.GetObject("btnexel.Image")));
            this.btnexel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnexel.Location = new System.Drawing.Point(201, 127);
            this.btnexel.Name = "btnexel";
            this.btnexel.Size = new System.Drawing.Size(60, 28);
            this.btnexel.TabIndex = 11;
            this.btnexel.Text = "Exel";
            this.btnexel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btnexel.Click += new System.EventHandler(this.btnexel_Click);
            // 
            // txt_manv
            // 
            this.txt_manv.Enabled = false;
            this.txt_manv.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txt_manv.Location = new System.Drawing.Point(50, 84);
            this.txt_manv.MaxLength = 50;
            this.txt_manv.Name = "txt_manv";
            this.txt_manv.Size = new System.Drawing.Size(46, 22);
            this.txt_manv.TabIndex = 2;
            this.txt_manv.Validated += new System.EventHandler(this.txt_manv_Validated);
            this.txt_manv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_manv_KeyDown);
            this.txt_manv.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_manv_KeyPress);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(5, 89);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(43, 13);
            this.label3.TabIndex = 13;
            this.label3.Text = "Mã NV:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lst_manv
            // 
            this.lst_manv.ColumnCount = 0;
            this.lst_manv.FormattingEnabled = true;
            this.lst_manv.Location = new System.Drawing.Point(8, 138);
            this.lst_manv.MatchBufferTimeOut = 1000;
            this.lst_manv.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
            this.lst_manv.Name = "lst_manv";
            this.lst_manv.Size = new System.Drawing.Size(55, 17);
            this.lst_manv.TabIndex = 15;
            this.lst_manv.TextIndex = -1;
            this.lst_manv.TextMember = null;
            this.lst_manv.ValueIndex = -1;
            this.lst_manv.Visible = false;
            // 
            // haison1
            // 
            this.haison1.Location = new System.Drawing.Point(39, 12);
            this.haison1.Name = "haison1";
            this.haison1.Size = new System.Drawing.Size(274, 68);
            this.haison1.TabIndex = 1;
            this.haison1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.haison1_KeyDown);
            // 
            // frmQuaTrinhCongTac
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(357, 165);
            this.Controls.Add(this.lst_manv);
            this.Controls.Add(this.haison1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txt_manv);
            this.Controls.Add(this.btnexel);
            this.Controls.Add(this.butThoat);
            this.Controls.Add(this.butIn);
            this.Controls.Add(this.butXem);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txt_hoten);
            this.ForeColor = System.Drawing.Color.DarkBlue;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmQuaTrinhCongTac";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Quá trình công tác";
            this.Load += new System.EventHandler(this.frmQuaTrinhCongTac_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void butThoat_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
        private void getngay()
        {
            tungay = haison1.tungay;
            denngay = haison1.denngay;
        }
		private void print(int prn)
		{	
		    DataSet dstmp=new DataSet();
            getngay();
			string sql=" SELECT trim(l.ho)||' '||trim(l.ten) as Hoten,b.tenbp as Tenbophan,";
			sql += " l.madv AS madv,l.manv AS manv,c.ndcv as Noidung,c.mucluong as luong,c.donvi,";
			sql += " to_char(c.ngayhl,'dd/mm/yyyy') as tu_ngay,to_char(c.denngay,'dd/mm/yyyy') as den_ngay,";
            sql += " to_char(now(),'dd') as Ngay,to_char(now(),'mm') as Thang,to_char(now(),'yyyy') as Nam";
            sql += " ,b.sothutu as stt_bp,c.nghenghiep as chuyenmon";
            sql += " from " + m_user + ".llnv l inner join " + m_user + ".congviec c on l.manv=c.manv";
            sql += " left join " + m_user + ".bophan b on l.madv=b.madv";
            sql += " left join "+m_user+".ctdaotao dt on l.nnghiep=dt.mactdt";
            sql += " left join "+m_user+".bangcap bc on l.td_nnghiep=bc.mabangcap";
            sql += " left join " + m_user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + m_user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.ngaynghi is null";// l.manv is not null ";
			if(txt_manv.Text!="") 
                sql+=" and l.manv='"+ txt_manv.Text +"'";        
            sql += " and to_timestamp(to_char(c.denngay,'dd/MM/yyyy'),'dd/MM/yyyy')<=to_timestamp('"+ denngay +"','dd/MM/yyyy')";
            sql += " and to_timestamp(to_char(c.ngayhl,'dd/MM/yyyy'),'dd/MM/yyyy')>=to_timestamp('" + tungay + "','dd/MM/yyyy')";			
            sql += " order by b.sothutu,l.manv,to_date(to_char(c.ngayhl,'dd/mm/yyyy'),'dd/mm/yyyy') ";
			ds=mDB.get_data(sql);
			ds.WriteXml("..\\xml\\h_QuaTrinhCongTac.xml",XmlWriteMode.WriteSchema);
			if (ds.Tables[0].Rows.Count>0)
			{
                switch (prn)
                {
                    case 1: frmReport f = new frmReport(mDB, ds, "QUÁ TRÌNH CÔNG TÁC", "h_QuaTrinhCongTac.rpt", false,user_dn);
                        f.ShowDialog(this);
                        break;
                    case 2: dstmp = exp_exel(ds);
                        exp.exp_excel_bccongtac(dstmp, lan.Change_language_MessageText("Báo Cáo Công Tác"));
                        break;
                }
				
			}
			else
				MessageBox.Show(lan.Change_language_MessageText("Nhân viên này chưa có quá trình công tác"),AccessData.Msg);
		}
        private DataSet exp_exel(DataSet dse)
        {
            string[] str = new string[] { "Mã DV","Mã Nhân Viên", "Họ Và Tên",  "Nội Dung", "Lương", "Ngày Hiệu Lực" };
            DataSet dse1 = new DataSet();
            dse1.Tables.Add();
            dse1.Tables[0].Columns.Add("madv", typeof(string));
            dse1.Tables[0].Columns.Add("manv", typeof(string));
            dse1.Tables[0].Columns.Add("Hoten", typeof(string));            
            dse1.Tables[0].Columns.Add("Noidung", typeof(string));
            dse1.Tables[0].Columns.Add("luong", typeof(string));
            dse1.Tables[0].Columns.Add("tu_ngay", typeof(string));
            //dse1.Tables[0].Columns.Add("mangach", typeof(string));
            //dse1.Tables[0].Columns.Add("Nam", typeof(string));
            //dse1.Tables[0].Columns.Add("Thang", typeof(string));
            

            //int stt = 0;
            DataRow nrow;
            foreach (DataRow r in dse.Tables[0].Rows)
            {
                //stt++;
                nrow = dse1.Tables[0].NewRow();
                nrow["madv"] = r["madv"].ToString();
                nrow["manv"] = r["manv"].ToString();
                nrow["Hoten"] = r["Hoten"].ToString();
                nrow["Noidung"] = r["Noidung"].ToString();
                nrow["luong"] = r["luong"].ToString();
                nrow["tu_ngay"] = r["tu_ngay"].ToString();
                //nrow["mangach"] = r["mangach"].ToString();
                //nrow["Nam"] = r["Nam"].ToString();
                //nrow["Thang"] = r["Thang"].ToString();                 
                dse1.Tables[0].Rows.Add(nrow);
            }
            return dse1;
        }
		private void butXem_Click(object sender, System.EventArgs e)
		{
			print(1);
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			print(1);
		}

		private void frmQuaTrinhCongTac_Load(object sender, System.EventArgs e)
		{
            m_user = mDB.user;
            //su dung control list
            ds_hoten = TenNV();
            lst_manv.DataSource = ds_hoten.Tables[0];
            lst_manv.DisplayMember = "manv";
            lst_manv.ValueMember = "hoten";          
		}

        private DataSet TenNV()
        {            
            string sql = "SELECT manv,ho||' '||ten AS hoten from " + mDB.user + ".llnv order by manv";
            return mDB.get_data(sql);
        }

		private void cbobBoPhan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode.ToString() == "Enter")
				SendKeys.Send("{Tab}");	
		}

		private void dgrQuaTrinhCT_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode.ToString() == "Enter")
				SendKeys.Send("{Tab}");
		}       
        private void btnexel_Click(object sender, EventArgs e)
        {
            print(2);
        }
        #region sử dụng control list

        private void txt_manv_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up))
            {
                this.lst_manv.Focus();
            }
            else if (e.KeyCode == Keys.Return)
            {
                if (this.lst_manv.Visible)
                {
                    this.lst_manv.Focus();
                }
                else
                {
                    SendKeys.Send("{Tab}");
                }
            }
        }
        private void txt_manv_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!IsNumeric(e.KeyChar.ToString()))
                e.Handled = true;
            if (e.KeyChar == 8 || e.KeyChar == '.')
                e.Handled = false;
        }
        private bool IsNumeric(string strNum)
        {
            int iNum;
            try
            {
                iNum = int.Parse(strNum);
                return true;
            }
            catch
            {
                return false;
            }
        }
        private void txt_hoten_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up))
            {
                this.lst_manv.Focus();
            }
            else if (e.KeyCode == Keys.Return)
            {
                if (this.lst_manv.Visible)
                {
                    this.lst_manv.Focus();
                }
                else
                {
                    SendKeys.Send("{Tab}");
                }
            }
        }
        private void loc_hoten(string hoten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)this.BindingContext[lst_manv.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "Hoten like '%" + hoten.Trim() + "%' ";
            }
            catch
            {

            }
        }
        private void txt_hoten_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.ActiveControl == this.txt_hoten)
                {
                    this.loc_hoten(txt_hoten.Text.Trim());
                    this.lst_manv.BrowseToTennv(txt_hoten, txt_manv, txt_hoten, 500);
                }
            }
            catch { };
        }

        private void haison1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
        DataRow[] dr_manv;

        private void txt_manv_Validated(object sender, EventArgs e)
        {
            
            if (txt_manv.Text.Trim() != "")
            {
                this.dr_manv = ds_hoten.Tables[0].Select("manv='" + txt_manv.Text.Trim() + "'");
                if (dr_manv != null)
                {
                    txt_hoten.Text = dr_manv[0]["Hoten"].ToString().Trim();
                }
                else
                {
                    txt_hoten.Text = "";
                }
            }
            
        }          
        private void lst_manv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                txt_manv_Validated(null, null);
            }
        }
        #endregion
    }
}
